package cn.ixinjiu.libraryMangement.dao.impl;

import cn.ixinjiu.libraryMangement.dao.IBookInfoDao;
import cn.ixinjiu.libraryMangement.entity.BookInfo;
import cn.ixinjiu.libraryMangement.utils.JDBCUtil;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by XinChen on 2023-05-22
 *
 * @TODO
 */
public class BookInfoDaoImpl implements IBookInfoDao {
    @Override
    public List<BookInfo> booksInfo() {
        try {
            ResultSet query = JDBCUtil.query("select * from book_info;", null);
            ArrayList<BookInfo> bookInfos = new ArrayList<>();
            while (query.next()) {
                int id = query.getInt("id");
                String bookName = query.getString("book_name");
                String bookPic = query.getString("book_pic");
                int sortId = query.getInt("sort_id");
                String author = query.getString("author");
                String publisher = query.getString("publisher");
                Date publisherAt = query.getDate("publisher_at");
                String isbn = query.getString("isbn");
                int status = query.getInt("status");
                BookInfo bookInfo = new BookInfo(id, bookName, bookPic, sortId, author, publisher, publisherAt, isbn, status);
                bookInfos.add(bookInfo);
            }
            return bookInfos;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int count() {
        try {
            ResultSet query = JDBCUtil.query("SELECT COUNT(*) as count FROM `book_info`", null);
            while (query.next()) {
                int count = query.getInt("count");
                return count;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return 0;
    }

    @Override
    public BookInfo getBookInfoById(int bid) {
        try {
            ResultSet query = JDBCUtil.query("select * from book_info where id = ?", new Object[]{bid});
            while (query.next()) {
                int id = query.getInt("id");
                String bookName = query.getString("book_name");
                String bookPic = query.getString("book_pic");
                int sortId = query.getInt("sort_id");
                String author = query.getString("author");
                String publisher = query.getString("publisher");
                Date publisherAt = query.getDate("publisher_at");
                String isbn = query.getString("isbn");
                int status = query.getInt("status");
                return new BookInfo(id, bookName, bookPic, sortId, author, publisher, publisherAt, isbn, status);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    @Override
    public int bookInfoAdd(BookInfo bookInfo) {
        String sql = "INSERT INTO `book_info`  VALUES (null, ?, ?, ?, ?, ?, ?, ?, ?);";
        return JDBCUtil.update(sql, new Object[]{bookInfo.getBookName(), bookInfo.getBookPic(), bookInfo.getSortId(), bookInfo.getAuthor(),
                bookInfo.getPublisher(), bookInfo.getPublisherAt(), bookInfo.getIsbn(), bookInfo.getStatus()});
    }

    @Override
    public int bookInfoUpd(BookInfo bookInfo) {
        String sql = "UPDATE `book_info` SET `book_name` = ?, `book_pic` = ?, `sort_id` = ?, `author` = ?, `publisher` = ?, `publisher_at` = ?, `isbn` = ?, `status` = ? WHERE `id` = ?;";
        return JDBCUtil.update(sql, new Object[]{bookInfo.getBookName(), bookInfo.getBookPic(), bookInfo.getSortId(), bookInfo.getAuthor(),
                bookInfo.getPublisher(), bookInfo.getPublisherAt(), bookInfo.getIsbn(), bookInfo.getStatus(), bookInfo.getId()});
    }

    @Override
    public int bookInfoDel(int id) {
        return JDBCUtil.update("delete from book_info where id = ?", new Object[]{id});
    }
}
